package models

import (
	"context"
	"gorm.io/gorm"
	"major-six/major/order/internal/svc"
	"major-six/major/order/order"
)

type Order struct {
	gorm.Model
	GoodsId     int                 `json:"goods_id" gorm:"type:int(11);"`
	OrderNo     string              `json:"order_no" gorm:"type:varchar(50);"`
	OrderCount  float64             `json:"order_count" gorm:"type:decimal(10,2);"`
	OrderStatus int                 `json:"order_status" gorm:"type:tinyint(1);comment:1已支付，0未支付，2已超时"`
	CreatedAt   string              `json:"created_at" gorm:"type:varchar(20);"`
	ctx         context.Context     `gorm:"all:-"`
	svcCtx      *svc.ServiceContext `gorm:"all:-"`
}

func NewOrderModel(ctx context.Context, svcCtx *svc.ServiceContext) *Order {
	return &Order{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (o *Order) Create(add Order) (info *order.OrderInfo, err error) {
	return info, WithMysql(func(db *gorm.DB) error {
		return db.Table("orders").Create(&add).Error
	})
}

func (o *Order) Update(id int, status int) (info *order.OrderInfo, err error) {
	return info, WithMysql(func(db *gorm.DB) error {
		return db.Table("orders").Where("id = ?", id).Update("order_status", status).Error
	})
}
